﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Model;


public partial class Profiles : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
            return;
        Session["UserType"] = 0;
        Session["UserId"] = 2;
        Session["pId"] = 1004;
        // 若session中没有userid， 重定向到登陆界面
        if (Session["UserId"] == null)
            Response.Redirect("Register.aspx");

        // 找到，显示用户信息
        int pId = GetPersonalInfoId();
        if (pId == -1)
            Response.Redirect("Home.aspx");
        else
            ShowProfiles(pId);

    }

    private void ShowProfiles(int pId)
    {
        PersonalInformation pInfo = PersonalInformationManage.GetPersonalInformationById(pId);
        if (pInfo == null)
            return;
        this.ModifyEmail.Text = pInfo.Email;
        this.ModifyName.Text = pInfo.Name;
        this.ModifyNickname.Text = pInfo.Nickname;
        this.ModifyPassword.Text = pInfo.Password;
        this.ModifyPhone.Text = pInfo.Phone;
    }

    protected int GetPersonalInfoId()
    {
        int userType = -1;
        int userId = -1;
        int pId = -1;
        if (Session["UserType"] != null)
            userType = (int)Session["UserType"];
        else
        {
            return -1;
        }
        if (Session["UserId"] != null)
            userId = (int)Session["UserId"];
        else
        {
            return -1;
        }
        // 获取PersonalInformationId
        if (userType == 0)
        {
            pId = HouseHolderManage.GetPersonalInformationIdById(userId);
        }
        else
        {
            pId = ClientManage.GetPersonalInformationIdById(userId);
        }

        return pId;
    }



    protected void Modify(object sender, EventArgs e)
    {
        // 获取PersonalInformationId
        int pId = GetPersonalInfoId();
        if (pId == -1)
            Response.Redirect("Home.aspx");

        PersonalInformation info = PersonalInformationManage.GetPersonalInformationById(pId);
        if (this.ModifyName.Text != "")
            info.Name = this.ModifyName.Text;
        // 禁止修改邮箱
        //if (this.ModifyEmail.Text != "")
        //    info.Email = this.ModifyEmail.Text;
        if (this.ModifyNickname.Text != "")
            info.Nickname = this.ModifyNickname.Text;
        if (this.ModifyPassword.Text != "")
            info.Password = this.ModifyPassword.Text;
        if (this.ModifyPhone.Text != "")
            info.Phone = this.ModifyPhone.Text;
        
        // 验证是否有效
        // 若有效则修改

            // 修改信息
            if (PersonalInformationManage.ModifyPersonalInformation(info) != null)
            {
                // 修改成功，提示
                this.Tip.Text = "更新成功";
            }
            else
            {
                // 修改失败，提示
                this.Tip.Text = "更新失败";
            }

            ShowProfiles(pId);
    }
}